#include "authdialog.h"
#include "ui_authdialog.h"
#include "uzonlineprovider.h"
#include <QDebug>

AuthDialog::AuthDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AuthDialog)
{
    ui->setupUi(this);
    connect(ui->authButton, SIGNAL(clicked()), SLOT(authSlot()));
}

void AuthDialog::authSlot()
{
    QHash<QString, QString> data;
    data["login"] = ui->loginEdit->text();
    data["password"] = ui->passwordEdit->text();

    //have only one provider, no switch here
    provider = new UzonlineProvider;
    connect(provider, SIGNAL(status(AbstractProvider::AuthResult)), SLOT(catchStatus(AbstractProvider::AuthResult)));
    provider->setData(data);
    provider->authorize();
}

void AuthDialog::catchStatus(AbstractProvider::AuthResult result)
{
    if(result != AbstractProvider::AuthSuccess) {
        provider->deleteLater();
        qDebug() << "error";
    }
    else {
        accept();
    }
}

AbstractProvider* AuthDialog::getProvider()
{
    return provider;
}

AuthDialog::~AuthDialog()
{
    delete ui;
}

